import processing.core.*;


public class Poikkileikkaus {

	int x,y,z,dz;
	PVector vertices[];
	PApplet p;
	int pts;
	float radius;
	float rotateX, rotateY;
	PMatrix3D asento;
	//esteet[]
	Esine este;

	public Poikkileikkaus(int x, int y, int z, float suuntaX, float suuntaY, int points, PApplet p)
	{
		radius = 130;
		this.pts = points;
		this.p = p;
		vertices = new PVector[pts+1];
		this.x = x;
		this.y = y;
		this.z = z;
		this.dz = 100;
		teeArvot();
		this.asento = new PMatrix3D(//matriisi johon poikkileikkauksen suhde edelliseen poikkileikkaukseen tallentuu
				1,0,0,0,
				0,1,0,0,
				0,0,1,0,
				0,0,0,1);
		this.rotateX = suuntaX;//kuinka paljon t�t� poikkileikkausta kierret��n edelliseen n�hden
		this.rotateY = suuntaY;
		asennoi(rotateX, rotateY, dz);
	}
	
	public void reset()
	{
		this.teeArvot();
	}
	public void liikuta(float speed)
	{
		asennoi(0,0, -speed);
		dz -= speed;
	}
	public void kerroMatriisilla(PMatrix3D matriisi)
	{
		for(int i = 0; i<this.vertices.length; i++)
		{
			//p.println("ennen "+ vertices[i]);
			matriisi.mult(vertices[i],vertices[i]);
			//p.println("jalkeen "+ vertices[i]);
		}
		
	}
	
	public int getEstePositio()
	{
		if(este != null)
		{
			return this.este.givePositio();
		}
		else return -10;//pelaaja ei voi olla -10 kohdassa niin se on turvallista palauttaa
	}
//	public PMatrix3D getAlkuMat()
//	{
//		this.teeArvot();
//		this.asento.reset();
//		this.asennoi(0, 0, dz);
//		return this.asento;
//	}
	public PMatrix3D getMat()
	{
		return this.asento;
	}

	public void teeArvot()
	{
		float angle = 0;
		for(int i=0; i<=pts; i++)
		{
			vertices[i] = new PVector();
			vertices[i].x = this.x+PApplet.sin(angle)*radius;
			vertices[i].y = this.y+PApplet.cos(angle)*radius;
			vertices[i].z = this.z;
			angle += 2*PApplet.PI / pts;
		}
	}
	
	public float getX(int i)
	{
		return vertices[i].x;
	}
	public float getY(int i)
	{
		return vertices[i].y;
	}
	public float getZ(int i)
	{
		return vertices[i].z;
	}
	
	public void asennoi(float a, float b, float dz)
	{
		this.asento.rotateY((float)a);
		this.asento.rotateX((float)b);
		this.asento.translate(0, 0, dz);
	}

	public PVector[] annaVertices()
	{
		return this.vertices;
	}
	//testailuun
	public void show()
	{
		p.strokeWeight(5);
		p.stroke(255);
		for(int i=0; i<=pts; i++)
		{
			p.point(vertices[i].x, vertices[i].y, vertices[i].z);
		}
		p.point(x, y, z);
	}
	
	public void asetaEsine(Esine e) {
		this.este = e;
	}
	
	public Esine annaEsine() {
		return this.este;
	}
	
//	public int annaX() {
//		return this.x;
//	}
//	
//	public int annaY() {
//		return this.y;
//	}
//	
//	public int annaZ() {
//		return this.z;
//	}
	
}
